import Vue from 'vue'
import { sessionStore } from 'store/dist/store.legacy'
const state = {
  url: process.env.VUE_APP_advertList_url,
}

const getters = {

}

const mutations = {
  setList (state, param) {
    console.log('param', param)
    state[`advert_${param.cat_id}`] = param.data
    sessionStore.set(`advert_${param.cat_id}`, param.data)
  }
}

const actions = {
  setList ({ state, commit }, cat_id) {
    let list = null
    list = state[cat_id]
    if (list && list.length > 0) {
      return
    }
    list = sessionStore.get(`advert_${cat_id}`)
    if (list && list.length > 0) {
      commit('setList', { data: list, cat_id: cat_id })
    } else {
      window.axios.post(state.url, { cat_id: cat_id }).then(ret => {
        if (ret.data.length > 0) {
          commit('setList', { data: ret.data, cat_id: cat_id })
        }
      })
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}